package model.conversations;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import view.input.InputWords;



public class Phrase{
	//id - Used for checking prereq
	//id$i1:i2:i3...$u1:u2:u3...$p1:p2:p3
	//whoareyoualex:Hello:Greetings:Yo Dawg
	//alex1$The name is Alex:My friends call me Alex:My name is Alex:$Hello|Sup|Holla:Alex|A-Dog|A Dog|Lex$whoareyoualex
	public String hint;
	public int phraseID;
	//finalReply indicates whether the conversation is exited if this question is answered
	private boolean finalPhrase;
	//indicates whether the phrase indicates whether the conversation has been successful
	private boolean successfulPhrase;
	//answered indicates whether the phrase has previously been ansered
	private boolean answered=false;
	//only be one prerequisite reply
	private int prerequisiteID;
	// Not sure why this must be an ArrayList, I should be able to keep it as a List
	public ArrayList<String> whatICanSay, // We will keep these in the form 'Hello|Sup|Holla:Alex|A-Dog|A Dog'
							 whatYouMustSay;
	     		 	  
	Random rand;
	private List<String> identifier1;
	private List<String> identifier2;
	private List<String> identifier3;
	int numberOfIdentifiers=0;
	        	
	public Phrase(int id,int prerequisite, String Hint,String iSay, String uSay, boolean fin,boolean success)
	{
		hint=Hint;
		phraseID=id;
		finalPhrase=fin;
		successfulPhrase=success;
		prerequisiteID=prerequisite;
		
		// I think this is what the answer was for...
		rand = new Random(42);
		whatICanSay = new ArrayList<String>();
		whatYouMustSay = new ArrayList<String>();
		//System.out.println("Creating phrase: "+id);
		
		identifier1 = new ArrayList<String>();
		identifier2 = new ArrayList<String>();
		identifier3 = new ArrayList<String>();
		String row;
		
		if( !iSay.isEmpty() ){
			for( String s : iSay.split(":")){
				whatICanSay.add(s);
			}
		}
		
		if( !uSay.isEmpty() ){
			for( String s : uSay.split(":")){
				whatYouMustSay.add(numberOfIdentifiers,s.toUpperCase());
				numberOfIdentifiers+=1;
			}
		}
		if(numberOfIdentifiers>0){
			row=whatYouMustSay.get(0);
			if(row.indexOf("|")>0){
				for( String s : row.split("\\|")){
					identifier1.add(s.toUpperCase());
				}
			}
			else{
				identifier1.add(row);
			}
			if(numberOfIdentifiers>1){
				row=whatYouMustSay.get(1);
					if(row.indexOf("|")>0){
						for( String s : row.split("\\|")){
							identifier2.add(s.toUpperCase());
						}
					}
					else{
						identifier2.add(row);
					}
				if(numberOfIdentifiers>2){
					row=whatYouMustSay.get(2);
					if(row.indexOf("|")>0){
						for( String s : row.split("\\|")){
							identifier3.add(s.toUpperCase());
						}
					}
					else{
						identifier3.add(row);
					}
				}
			}
		}
		
    }

	public int getID(){
		return phraseID;
	}
	
	public int getPrerequesite(){
		return prerequisiteID;
	}
	
	public boolean getFinalPhraseStatus(){
		return finalPhrase;
	}

	
	public boolean getSuccessfulPhraseStatus(){
		return successfulPhrase;
	}
	
	public boolean getAnsweredStatus(){
		return answered;
	}
	
	public void setAnsweredStatus(boolean ans){
		answered=ans;
	}
	
	// This is interesting as you've fleshed out each
	// possible outcome.
	public boolean match(InputWords input){
		if(numberOfIdentifiers==1){
			for(String s:identifier1){
				if(input.getWord1().equals(s)){
					return true;
				}
			}
		}
		else if(numberOfIdentifiers==2){
			for(String s:identifier1){
				if(input.getWord1().equals(s)){
					for(String t:identifier2){
						if(input.getWord2().equals(t)){
							return true;
						}
					}
				}
			}
		}
		else if(numberOfIdentifiers==3){
			
			for(String v:identifier1){
				
				if(input.getWord1().equals(v)){
					
					for(String t:identifier2){
						if(input.getWord2().equals(t)){
							System.out.println("check");
							for(String x:identifier3){
								if(input.getWord3().equals(x)){
									return true;
								}
							}
						}
					}
				}
			}
		}
		return false;
	}

	public String tellMeSomething(){
		// Assumes we always have something to say, otherwise / by 0
		return whatICanSay.get(rand.nextInt(42) % whatICanSay.size());
	}
}


